import koaSession from "koa-session";
import Koa, { Context } from 'koa'
import { KoaMiddlewareInterface, Middleware } from "routing-controllers";
import { Service } from "typedi";
import { NotLoginException } from "@/exception/exception";

const NO_AUTH_KEY = '_noAuth';

const WHITE_LIST = [
  '/auth/login',
]

@Service()
@Middleware({ type: 'before' })
export class AuthCheckMiddleware implements KoaMiddlewareInterface {
  async use(context: Context, next: (err?: any) => Promise<any>): Promise<any> {
    if (!context.session?.user && !context[NO_AUTH_KEY]) {
      // context.throw(401, '请先登录', { message1: '请先登录', success: false });
    }
    await next();
  }
}
